﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace XHFrame
{
    public class ModuleManager : Singleton<ModuleManager>
    {

        private Dictionary<string, BaseModule> dicModules = null;


        public override void Init()
        {
            base.Init();
            dicModules = new Dictionary<string, BaseModule>();
        }


        #region 返回Model
        /// <summary>
        /// 返回Module
        /// </summary>
        /// <param name="key">Model的名字</param>
        /// <returns></returns>

        public BaseModule Get(string key)
        {
            if (dicModules.ContainsKey(key))
            {
                return dicModules[key];
            }
            return null;
        }
        /// <summary>
        /// 返回Module
        /// </summary>
        /// <typeparam name="T">Model继承类的名字</typeparam>
        /// <returns></returns>
        public T Get<T>() where T : BaseModule
        {
            Type t = typeof(T);
            if (dicModules.ContainsKey(t.ToString()))
                return dicModules[t.ToString()] as T;
            return null;
        }
        #endregion


        #region 注册Model

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="module"></param>
        public void Register(BaseModule module)
        {
            Type t = module.GetType();
            Register(t.ToString(), module);
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="key"></param>
        /// <param name="module"></param>
        public void Register(string key, BaseModule module)
        {
            if (!dicModules.ContainsKey(key))
                dicModules.Add(key, module);
        }

        #endregion


        public void RegisterModes(Type ModlueType)
        {
            LodeModule(ModlueType);
        }

        public void LodeModule(Type modeleType)
        {
            BaseModule bm = Activator.CreateInstance(modeleType) as BaseModule;
            bm.Load();
        }


        #region 注销Model
        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="key"></param>
        public void UnRegister(string key)
        {
            if (dicModules.ContainsKey(key))
            {
                BaseModule module = dicModules[key];
                module.Release();
                dicModules.Remove(key);
                module = null;
            }
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="module"></param>
        public void UnRegister(BaseModule module)
        {
            Type t = module.GetType();
            UnRegister(t.ToString());
        }

        /// <summary>
        /// 注销全部
        /// </summary>
        public void UnRegisterAll()
        {
            List<string> _keyList = new List<string>(dicModules.Keys);
            for (int i = 0; i < _keyList.Count; i++)
            {
                UnRegister(_keyList[i]);
            }
            dicModules.Clear();
        }
        #endregion


    }
}
